package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.avic.rong.MainActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.bean.MinePageEvent;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderListActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";
    public static String ARG_FROM = "arg_from";

    @BindView(R.id.webview)
    BridgeWebView webView;

    private String comeFrom = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
            comeFrom = getIntent().getStringExtra(ARG_FROM);
        }

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }


        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                data = data.replace("\\","");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                if (MyApplication.getInstance().userLoginBean != null && !currentUrl.contains("userIdSign")){
                    if (currentUrl.contains("?")){
                        currentUrl = currentUrl +"&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }else{
                        currentUrl = currentUrl +"?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }
                }
//                http://m.rongrong.huazhi365.com/app/member/invoiceDetail?invId=102438&userIdSign=212909

                if (currentUrl.contains("/toPay")){//去支付
                    startActivity(ToPayActivity.queryIntent(OrderListActivity.this,currentUrl));
                }else if(currentUrl.contains("order/orderDetail")){//订单详情
                    startActivity(OrderDetailActivity.queryIntent(OrderListActivity.this,currentUrl));
                }else if(currentUrl.contains("member/invoiceDetail")){//发票详情
                    startActivity(VoiceDetailActivity.queryIntent(OrderListActivity.this,currentUrl));
                }else if(currentUrl.contains("order/getUploadDiv")){//上传支付凭证
                    startActivity(UploadBankCertificateActivity.queryIntent(OrderListActivity.this,currentUrl));
                }else{
                    startActivity(CriterionDetailActivity.queryIntent(OrderListActivity.this,currentUrl));
                }
            }
        });


        //查看取货地址
        webView.registerHandler("pushNoHeadVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                data = data.replace("\\","");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(currentUrl));
                startActivity(intent);
            }
        });

        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                if (webView.canGoBack()){
                    webView.goBack();
                }else{
                    if(!TextUtils.isEmpty(comeFrom) && comeFrom.equals(ToPayActivity.ARG_SOURCE)){
                        EventBus.getDefault().postSticky(new MinePageEvent());
                        Intent intent = MainActivity.queryIntent(OrderListActivity.this);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }else{
                        finish();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().removeAllStickyEvents();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
            webView.goBack();
        }
        return super.onKeyDown(keyCode, event);
    }

    public static Intent queryIntent(Context context, String url){
        Intent intent = new Intent(context,OrderListActivity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }

    public static Intent queryIntent(Context context, String url, String from){
        Intent intent = new Intent(context,OrderListActivity.class);
        intent.putExtra(ARG_URL,url);
        intent.putExtra(ARG_FROM,from);
        return intent;
    }


}
